﻿using System;
using System.Windows.Controls;
using System.Windows.Input;
using Kang.DbDoc.Enums;
using Kang.DbDoc.Extended;
using Kang.DbDoc.Models;
using Kang.DbDoc.ViewModels;
using MahApps.Metro.SimpleChildWindow;

namespace Kang.DbDoc.Views
{
    /// <summary>
    /// DatabaseManagerPage.xaml 的交互逻辑
    /// </summary>
    public partial class DatabaseManagerPage : ChildWindow
    {
        public DatabaseManagerPage()
        {
            InitializeComponent();

            cmb_database_type.SelectionChanged += Cmb_database_type_OnSelectionChanged;
        }
        
        private void Datagrid_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var dg = sender as DataGrid;
            if (dg == null)
            {
                return;
            }
            var p = e.GetPosition(dg);
            var database = dg.GetElementFromPoint<DatabaseModel>(p);
            if (database == null)
            {
                return;
            }
            try
            {
                DatabaseManagerPageViewModel model = this.DataContext as DatabaseManagerPageViewModel;
                model?.SaveDatabaseToLocalDb(database);
                this.Close(database);
            }
            catch (Exception ex)
            {
                App.ShowMessageAsync("错误", ex.Message);
            }
            finally
            {
                e.Handled = true;
            }
        }

        private void Cmb_database_type_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (sender is ComboBox != true)
            {
                return;
            }
            ComboBox cmb = sender as ComboBox;
            if (cmb.Uid != "69216A0F-6E06-4E15-8970-9836207769D8")
            {
                return;
            }
            if (cmb.SelectedItem is DatabaseTypeEnum != true)
            {
                return;
            }
            DatabaseManagerPageViewModel model = this.DataContext as DatabaseManagerPageViewModel;
            if (model == null)
            {
                return;
            }
            if (model.Database == null)
            {
                return;
            }
            DatabaseTypeEnum? databaseType = cmb.SelectedItem as DatabaseTypeEnum?;
            if (model.Database.Id == 0)
            {
                model.Database.SetDefaultValue(databaseType);
            }
            switch (databaseType)
            {
                case DatabaseTypeEnum.Oracle:
                case DatabaseTypeEnum.SqlServer:
                    model.IsHostCollapsed = false;
                    model.IsUsernameCollapsed = false;
                    model.IsPasswordCollapsed = false;
                    model.IsSslModeCollapsed = true;
                    break;
                case DatabaseTypeEnum.MySql:
                    model.IsHostCollapsed = false;
                    model.IsUsernameCollapsed = false;
                    model.IsPasswordCollapsed = false;
                    model.IsSslModeCollapsed = false;
                    break;
                case DatabaseTypeEnum.SQLite:
                    model.IsHostCollapsed = true;
                    model.IsUsernameCollapsed = true;
                    model.IsPasswordCollapsed = false;
                    model.IsSslModeCollapsed = true;
                    break;
                case DatabaseTypeEnum.PostgreSQL:
                case DatabaseTypeEnum.DB2:
                    model.IsHostCollapsed = false;
                    model.IsUsernameCollapsed = false;
                    model.IsPasswordCollapsed = true;
                    model.IsSslModeCollapsed = true;
                    break;
            }
            model.Database.RefreshConnectionString();
            e.Handled = true;
        }
    }
}
